Javascript toFixed 不舍入
全部标签 我想将数字1732舍入到最接近的十,十万。我尝试使用数学轮函数,但它只为浮点和double编写。如何为整数做到这一点?java有什么功能吗? 最佳答案 您想使用哪种舍入机制?对于正数,这是一种原始方法:introundedNumber=(number+500)/1000*1000;这将带来类似1499到1000和1500到2000的值。如果你可以有负数:intoffset=(number>=0)?500:-500;introundedNumber=(number+offset)/1000*1000;
我正在使用java.util.Random来生成随机高斯。我需要将此高斯转换为浮点值。但是gaussian是double的,所以我需要一些方法来进行舍入,然后将其转换为float。我需要四舍五入到最接近的整数,四舍五入。这是我的问题:如何? 最佳答案 floatb=(float)Math.ceil(a);或者floatb=(float)Math.round(a);取决于您的意思是“四舍五入”(round)还是“向上舍入”(ceil)。注意将doublefloat转换为float时会丢失精度,但这在这里应该不是问题。
我发现DecimalFormat中的舍入不一致java7和java8之间的类。这是我的测试用例:DecimalFormatTest.javaimportjava.text.DecimalFormat;publicclassDecimalFormatTest{publicstaticvoidmain(String[]args){DecimalFormatformat=(DecimalFormat)DecimalFormat.getInstance();format.setDecimalSeparatorAlwaysShown(true);format.setMinimumFraction
我尝试了以下,doubledoubleVal=1.745;doubledoubleVal1=0.745;BigDecimalbdTest=newBigDecimal(doubleVal);BigDecimalbdTest1=newBigDecimal(doubleVal1);bdTest=bdTest.setScale(2,BigDecimal.ROUND_HALF_UP);bdTest1=bdTest1.setScale(2,BigDecimal.ROUND_HALF_UP);System.out.println("bdTest:"+bdTest);//1.75System.out.
如何将double值转换为int执行以下操作:DoubleIfx=4.97542.Converttointx=4.DoubleIfx=4.23544.Converttointx=4.也就是说,答案总是向下取整。 最佳答案 如果你明确地将castdouble转换为int,小数部分将被截断。例如:intx=(int)4.97542;//gives4onlyintx=(int)4.23544;//gives4only此外,您还可以使用Math.floor()方法对值进行四舍五入,以防您希望返回double值。
如何在Java中将float四舍五入到下一个整数值?假设2.1-->33.001-->44.5-->57.9-->8 最佳答案 您应该查看java的数学包中的上限取整:Math.ceil编辑:添加了Math.ceil的javadoc。可能值得阅读Math中的所有方法。http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#ceil%28double%29publicstaticdoubleceil(doublea)Returnsthesmallest(closestt
这个问题在这里已经有了答案:HowtoroundanumbertondecimalplacesinJava(39个回答)关闭3年前。这是我将double舍入到小数点后2位的操作:amount=roundTwoDecimals(amount);publicdoubleroundTwoDecimals(doubled){DecimalFormattwoDForm=newDecimalFormat("#.##");returnDouble.valueOf(twoDForm.format(d));}如果金额=25.3569或类似的值,这很有效,但如果金额=25.00或金额=25.0,那么我得到
我想将float四舍五入到给定的精度,例如:0.051iwanttoconvertitto0.10.049iwanttoconvertitto0.00.56iwanttoconvertitto0.60.54iwanttoconvertitto0.5我无法更好地解释它,但这样做的原因是将点位置(如0.131f、0.432f)转换为网格中瓦片的位置(如0.1f、0.4f)。 最佳答案 只要您的网格是规则的,只需找到从整数到此网格的转换即可。所以假设你的网格是0.20.40.6...那你绕过去floatround(floatf){retu
我在我的x86VM(32位)上发现了以下程序:#includevoidfoo(longdoublex){inty=x;printf("(int)%Lf=%d\n",x,y);}intmain(){foo(.9999999999999999999728949456878623891498136799780L);foo(.999999999999999999972894945687862389149813679978L);return0;}产生以下输出:(int)1.000000=1(int)1.000000=0Ideonealsoproducesthisbehavior.编译器做了什么来
我错过了什么吗?varsomeNumber=123.456;someNumber=someNumber.toFixed(2);alert(typeof(someNumber));//alertsstring为什么.toFixed()返回一个字符串?我想将数字四舍五入为2位小数。 最佳答案 Number.prototype.toFixed是一个功能,旨在在打印出来之前格式化一个数字。它来自toString的家庭,toExponential和toPrecision.要对数字进行四舍五入,您可以这样做:someNumber=42.008;